package com.teamview.service;

import com.teamview.domain.Employee;
import com.teamview.domain.*;

import static com.teamview.service.Data.*;//导入静态

/**
 * @author Lenl
 * @create 2021-06-01 22:46
 * @Description 负责将Data中的数据封装到Employee[]数组中，同时提供相关操作Employee[]的方法
 * @version v1.0
 */
public class NameListService {
    private Employee[] employees;

    /**
     * 给数组及数组元素进行初始化
     */
    public NameListService(){
        //根据项目提供的Data类构造相应大小的数组
        //再根据Data中的数据构建不同的对象
        //最后将对象存于数组中
        employees=new Employee[EMPLOYEES.length];
        for (int i=0;i<EMPLOYEES.length;i++){
            int type=Integer.parseInt(EMPLOYEES[i][0]);
            //获取Employee的四个基本信息
            int id= Integer.parseInt(EMPLOYEES[i][1]);
            String name=EMPLOYEES[i][2];
            int age=Integer.parseInt(EMPLOYEES[i][3]);
            double salary=Double.parseDouble(EMPLOYEES[i][4]);//薪水
            Equipment equipment=null;//设备
            double bonus;//奖金
            int stock;//股票
            Status st=null;
            switch(type){
                case EMPLOYEE:
                    employees[i]=new Employee(id,name,age,salary);
                    break;
                case PROGRAMMER:
                    equipment=createEquipment(i);
                    employees[i]=new Programmer(id,name,age,salary,equipment);
                    break;
                case DESIGNER:
                    equipment=createEquipment(i);
                    bonus=Double.parseDouble( EMPLOYEES[i][5]);
                    employees[i]=new Designer(id,name,age,salary,equipment,bonus);
                    break;
                case  ARCHITECT:
                    equipment=createEquipment(i);
                    bonus=Double.parseDouble( EMPLOYEES[i][5]);
                    stock=Integer.parseInt(EMPLOYEES[i][6]);
                    employees[i]=new Architect(id,name,age,salary,equipment,bonus,stock);
                    break;
            }
        }
    }
    //获取指定位置的设备
    private Equipment createEquipment(int index){
        int type=Integer.parseInt( EQUIPMENTS[index][0]);
        String modelOrName=EQUIPMENTS[index][1];
        switch(type){
            case PC:
                return new PC(modelOrName,EQUIPMENTS[index][2]);
            case NOTEBOOK:
                return new NoteBook(modelOrName,EQUIPMENTS[index][2]);
            case PRINTER:
                return new Printer(modelOrName,EQUIPMENTS[index][2]);
        }
        return null;
    }

    /**
     * 获取当前所有员工
     * @return 所有员工对象的数组
     */
    public  Employee[] getAllEmployees(){
        return employees;
    }

    /**
     * 获取指定id的员工
     * @param id
     * @return
     */
    public  Employee getEmployee(int id) throws TeamException{
        for(int i=0;i<employees.length;i++){
            if(employees[i].getId()==id){//若是对象，需要使用equals
                return employees[i];
            }
        }
        throw new TeamException("找不到指定的员工！");
    }
}
